DSA Homework 4

Roger Jang


Due date: 20160508 23:59:59

Simulation of Stock Trading

Problem definition

An online computer system for trading stock needs to process bids of the form ¡§buy 100 shares at $x$ each¡¨ or ¡§sell 50 shares at $y$ each.¡¨ A buy bid for $x$ can only be processed if there is an existing sell bid with price $y$ such that $y \leq x$. Likewise, a sell bid for $y$ can only be processed if there is an existing buy bid with price $x$ such that $x \geq y$.

If a buy or sell bid is entered but cannot be processed, it must wait for a future bid that allows it to be processed. Write a program that allows for buy and sell bids to be entered in O(log n) time, independent of whether or not they can be immediately processed.

The input file lists the bids in chronicle order, with the following fields:

bidId clientId action price shareCount These fields are described next: An typical example of input file is as follows: 0 983 1 114 2 1 741 0 128 41 2 816 0 183 68 3 244 1 171 100 4 712 1 125 1 5 543 1 127 61 6 934 1 144 74 7 234 0 173 68 8 48 1 181 58 9 883 0 139 39 . . . The output lists all the possible transactions in chronicle order, with the following fields: transId buyClientId sellClientId transPrice transShareCount

For instance, the output corresponding to the previous example input is shown next:

0 741 983 114 2 1 816 244 171 68 2 741 712 125 1 3 741 543 127 38 4 234 543 127 23 5 234 934 144 45 . . . Fields in both input and output are separated by tab.

Requirements & suggestions

Test cases

  1. No. of bids = 10: input1.txt, output1.txt
  2. No. of bids = 1000: input2.txt, output2.txt
  3. No. of bids = 10000: input3.txt, output3.txt
  4. No. of bids = 1000 (with "cancel"): input6.txt, output6.txt
  5. No. of bids = 10000 (with "cancel"): input7.txt, output7.txt
The time limit for each test case is 4 second.

Submission guidelines

Please submit your code with GitHub as directed in the GitHub submission guide. Your directory structure (under GitHub repository) should be:
  1. hw4/*, your source code.
  2. hw4/Makefile, where the TAs can use the command ¡¨make¡¨ on CSIE R217 linux machines to compile your code, and ¡¨make run¡¨ to run your program.
You have to make sure your code can be compiled on CSIE R217 linux machines with your Makefile and run normally since we will test your program on CSIE R217 linux machines.